package com.thejupitech;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;



public class BookDetailsActivity extends Activity {

	private StoreApdater adapter;
	private String isbnReceiver;
	ArrayList<HashMap<String, String>> myList = new ArrayList<HashMap<String,String>>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_details_layout);				
		final TextView tvTitle = (TextView)findViewById(R.id.title_details);
		final TextView tvISBN = (TextView)findViewById(R.id.isbn_details);
		final TextView tvAuthor = (TextView)findViewById(R.id.author_details);
		final ImageView bookIcon = (ImageView)findViewById(R.id.book_details_icon);	
		
		Bundle bleReceiver = this.getIntent().getExtras();
		isbnReceiver = bleReceiver.getString("isbn key");
		
		String strXml = XmlBookParser.getBooksXml(isbnReceiver, "http://booksearch.sg/books/book/keyword/xml");
		strXml = strXml.replace("&", "&amp;");
		Document doc = XmlBookParser.getXmlFromString(strXml);		
		Element bookDetails =(Element) doc.getElementsByTagName("book").item(0);
		
		String titleBook = XmlBookParser.getValue(bookDetails, "book-title");
		tvTitle.setText("Book title: "+ titleBook);
		if(titleBook.length() > 40) {
			titleBook = titleBook.substring(0, 39);
			titleBook +="...";
		}
		setTitle( titleBook);
		tvISBN.setText("ISBN: " + isbnReceiver);
		tvAuthor.setText("Author: " + XmlBookParser.getValue(bookDetails, "author"));
		NodeList nodes = doc.getElementsByTagName("store");
		for(int i = 0; i < nodes.getLength(); i++) {
			HashMap<String, String> temp = new HashMap<String, String>();
			Element e = (Element)nodes.item(i);

			temp.put("store name",XmlBookParser.getValue(e, "store-name"));
			temp.put("book url",XmlBookParser.getValue(e, "book-url"));

			temp.put("price", "Price: $" + XmlBookParser.getValue(e, "price"));
			temp.put("shipping", "Shipping: $" + XmlBookParser.getValue(e, "shiping"));
			temp.put("total", "Total: $" + XmlBookParser.getValue(e, "total"));
			temp.put("store logo", XmlBookParser.getValue(e, "store_logo"));
			
			myList.add(temp);
		}
		
		adapter = new StoreApdater(BookDetailsActivity.this, myList);
		adapter.imageLoader.DisplayImage(XmlBookParser.getValue(bookDetails, "image"), bookIcon);
		final ListView lv = (ListView)findViewById(R.id.list_stores);
		lv.setAdapter(adapter);
		
	}
	
	private class StoreApdater extends LazyAdapter {
		private LayoutInflater inflater=null;
		private Activity activity;
		private ArrayList<HashMap<String, String>> storeList;
		public StoreApdater(Activity a,
				ArrayList<HashMap<String, String>> bookList) {
			super(a, bookList);
			this.activity = a;
			this.storeList = bookList;
			inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			imageLoader=new ImageLoader(activity.getApplicationContext());
		}
		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			View vi = convertView;
			StoreWrapper storeWrapper=null;
			if(vi == null) {
				vi = inflater.inflate(R.layout.view_store_layout, null);
				storeWrapper = new StoreWrapper(vi);
				vi.setTag(storeWrapper);
			} else {
				storeWrapper = (StoreWrapper) vi.getTag();
			}
			
			imageLoader.DisplayImage(storeList.get(position).get("store logo"), storeWrapper.getStoreIcon());
			storeWrapper.getStoreName().setText(storeList.get(position).get("store name"));
			storeWrapper.getPrice().setText(storeList.get(position).get("price"));
			storeWrapper.getShipping().setText(storeList.get(position).get("shipping"));
			storeWrapper.getTotal().setText(storeList.get(position).get("total"));	
			storeWrapper.getBuyButton().setOnClickListener(new OnClickListener() {				
				public void onClick(View v) {				
					String url = storeList.get(position).get("book url");
					Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
					startActivity(i);
				}
			});					
			return vi;
		}
				
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		adapter.imageLoader.clearCache();
		super.onBackPressed();
	}
	
}
